package com.yamanyar.weebill.view.converters;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.yamanyar.weebill.domain.ActionPath;
import com.yamanyar.weebill.service.ActionPathService;

/**
 * This converter is spring managed bean due to need of loading the entity with
 * the given id.
 * 
 * Created in Jul 5, 2008.
 * 
 * @author Kaan Yamanyar
 */
@Component("actionPathConverter")
@Scope("request")
public class ActionPathConverter implements Converter {
	private ActionPathService actionPathService;

	@Autowired
	public ActionPathConverter(ActionPathService actionPathService) {
		this.actionPathService = actionPathService;
	}

	public Object getAsObject(FacesContext context, UIComponent component,
			String value) throws ConverterException {
		ActionPath actionPath = actionPathService.getActionPathWithID(new Long(
				value));
		return actionPath;
	}

	public String getAsString(FacesContext context, UIComponent component,
			Object value) throws ConverterException {
		if (value == null)
			return null;
		ActionPath actionPath = (ActionPath) value;
		if (actionPath.getId() == null)
			return null;
		return actionPath.getId().toString();
	}

	public ActionPathService getActionPathService() {
		return actionPathService;
	}

	public void setActionPathService(ActionPathService actionPathService) {
		this.actionPathService = actionPathService;
	}

}
